package com.ushier.shop.fruits.controller;

import com.ushier.shop.fruits.bean.Fruit;
import com.ushier.shop.fruits.bean.ResponseMessageBean;
import com.ushier.shop.fruits.dao.FruitDAO;
import com.ushier.shop.fruits.util.StringUtil;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.List;

@WebServlet("/manage/fruit")
public class ManageFruitServlet extends HttpServlet {

    private FruitDAO fruitDAO = new FruitDAO();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader("content-type","text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();

        String action = req.getParameter("action");

        String id = null;
        String name = null;
        String desc = null;
        String price = null;
        String pic = null;
        switch (action){
            case "list":
                List<Fruit> list = fruitDAO.list();
                list.forEach(apple -> {
                    String uploadPath = "http://localhost:8080" + File.separator + UploadServlet.UPLOAD_DIRECTORY + File.separator;
                    apple.setPic(uploadPath + apple.getPic());

                });
                out.print(JSONObject.fromObject(ResponseMessageBean.success(list)));
                return;
            case "insert":
                name = req.getParameter("name");
                desc = req.getParameter("desc");
                price = req.getParameter("price");
                pic = req.getParameter("pic");
                if(StringUtil.isNull(name) || StringUtil.isNull(desc) || StringUtil.isNull(price) || StringUtil.isNull(pic)){
                    out.print(JSONObject.fromObject(ResponseMessageBean.error("请补全数据")));
                    return;
                }
                boolean insert = fruitDAO.insert(name, desc, new BigDecimal(price), pic);
                out.print(JSONObject.fromObject(ResponseMessageBean.success(insert)));
                return;
            case "edit":
                id = req.getParameter("id");
                if(!StringUtil.isNull(id)){
                    Fruit fruit = fruitDAO.get(Integer.valueOf(id));
                    req.setAttribute("fruit", fruit);
                    req.getRequestDispatcher("/manage/fruit/edit.jsp").forward(req, resp);
                    return;
                }
                break;
            case "update":
                id = req.getParameter("id");
                name = req.getParameter("name");
                desc = req.getParameter("desc");
                price = req.getParameter("price");
                pic = req.getParameter("pic");
                if(StringUtil.isNull(id) || StringUtil.isNull(name) || StringUtil.isNull(desc) || StringUtil.isNull(price) || StringUtil.isNull(pic)){
                    out.print(JSONObject.fromObject(ResponseMessageBean.error("请补全数据")));
                    return;
                }
                boolean update = fruitDAO.update(Integer.valueOf(id), name, desc, new BigDecimal(price), pic);
                out.print(JSONObject.fromObject(ResponseMessageBean.success(update)));
                return;
            case "delete":
                id = req.getParameter("id");
                if(StringUtil.isNull(id)){
                    out.print(JSONObject.fromObject(ResponseMessageBean.error("请补全数据")));
                    return;
                }
                boolean delete = fruitDAO.delete(Integer.valueOf(id));
                out.print(JSONObject.fromObject(ResponseMessageBean.success(delete)));
                return;
            default:
                break;
        }


    }
}
